const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require("cors")

const adminAuth = require("./middlewares/admin-auth")
const userAuth = require("./middlewares/user-auth")

require("dotenv").config()
const createError = require("http-errors")
const errorHandler = require("./middlewares/error-handler")

const adminUsersRouter = require("./routes/admin/users")
const adminCategoriesRouter = require("./routes/admin/categories")
const adminAuthRouter = require("./routes/admin/auth")

const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const categoriesRouter = require("./routes/categories")
const tagsRouter = require("./routes/tags")
const postsRouter = require("./routes/posts")
const commentsRouter = require("./routes/comments")
const uploadsRouter = require("./routes/uploads")
const authRouter = require("./routes/auth")


const app = express();

app.use(cors());

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use("/admin/users", adminAuth, adminUsersRouter)
app.use("/admin/categories", adminCategoriesRouter)
app.use('/admin/auth', adminAuthRouter)

app.use('/', indexRouter);
app.use('/users', userAuth, usersRouter);
app.use("/categories", categoriesRouter)
app.use("/tags", tagsRouter)
app.use('/posts', postsRouter)
app.use("/comments", commentsRouter)
app.use("/uploads", uploadsRouter)
app.use("/auth", authRouter)

app.use(errorHandler)

module.exports = app;
